package everydayGrade.t4;

import java.util.Arrays;

/**
 * @Author: Siantar
 * @Date: 2023-11-22-21:10
 * @Description: 1.0
 */
public class T2304 {
    public int minPathCost(int[][] grid, int[][] moveCost) {
        int n = grid.length, m = grid[0].length;
        int[] dp = Arrays.copyOf(grid[0], m);
        for (int i = 1; i < n; i++) {
            int[] dpNext = new int[m];
            Arrays.fill(dpNext, Integer.MAX_VALUE);
            for (int j = 0; j < m; j++) {
                for (int k = 0; k < m; k++) {
                    dpNext[j] = Math.min(dpNext[j], dp[k] + moveCost[grid[i-1][k]][j]+grid[i][j]);
                }
            }
            dp=dpNext;
        }
        return Arrays.stream(dp).min().getAsInt();
    }
}